package com.itheima.ms.service;

import com.itheima.ms.anno.DS;
import com.itheima.ms.enums.DBRoleEnum;
import com.itheima.ms.mapper.TransferMapper;
import com.itheima.ms.pojo.BizUser;
import com.itheima.ms.tl.DataSourceTypeTL;
import com.itheima.ms.tools.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 */
@Service
@Transactional
public class TransferService {
    @Autowired
    private TransferMapper transferMapper;

    @Autowired
    private SnowFlake snowFlake;

    @DS(value = DBRoleEnum.SLAVE)
    public List<BizUser> findUsers(){
        DBRoleEnum dbRole = DataSourceTypeTL.getDbRole();
        return transferMapper.findAllUser();
    }

    @DS(value = DBRoleEnum.MASTER)
    public void updateUser(BizUser user){
        DBRoleEnum dbRole = DataSourceTypeTL.getDbRole();
        List<BizUser> users = findUsers();
        DBRoleEnum dbRole02 = DataSourceTypeTL.getDbRole();
        System.out.println(users);
        transferMapper.updateUser(user);
    }

    public void insertUser(BizUser user){
        DBRoleEnum dbRole = DataSourceTypeTL.getDbRole();
        user.setId(snowFlake.nextId());
        transferMapper.insertUser(user);
    }
}
